package com.softWang.system.controller;

import com.softWang.common.aop.annotation.SysOperationLog;
import com.softWang.system.constants.SysModel;
import com.softWang.system.controller.vo.systemArea.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

import com.softWang.common.response.PageBean;
import com.softWang.common.request.Idparam;
import com.softWang.common.response.ResponseResult;
import com.softWang.system.po.SystemArea;
import com.softWang.system.service.SystemAreaService;

@Api(value="行政区域管理",tags="行政区域管理")
@Validated
@RestController
@RequestMapping("/system/systemArea")
public class SystemAreaController {
     @Resource
     private SystemAreaService systemAreaService;

     @ApiOperation(value="列表(分页)")
     @PreAuthorize("@ss.hasPermi('system:area:query')")
     @RequestMapping(value = "/query", method = RequestMethod.POST)
     public ResponseResult<PageBean<SystemAreaQueryAllResult>> queryall(@RequestBody @Validated SystemAreaQueryAllParam param, HttpServletRequest request) {
         List<SystemAreaQueryAllResult> results=systemAreaService.queryall(param);
         PageBean page=new PageBean(results);
         return ResponseResult.success(page);
     }

     @ApiOperation(value="新增")
     @PreAuthorize("@ss.hasPermi('system:area:add')")
     @SysOperationLog(value = "新增", model = SysModel.AREA_MANAGE)
     @RequestMapping(value = "/add", method = RequestMethod.POST)
     public ResponseResult<Long> add(@RequestBody @Validated SystemAreaAddParam param, HttpServletRequest request) throws Exception {
         Long id = systemAreaService.add(param);
         return ResponseResult.success(id);
     }

     @ApiOperation(value="修改")
     @PreAuthorize("@ss.hasPermi('system:area:edit')")
     @SysOperationLog(value = "修改", model = SysModel.AREA_MANAGE)
     @RequestMapping(value = "/edit", method = RequestMethod.POST)
     public ResponseResult<Long> edit(@RequestBody @Validated SystemAreaEditParam param, HttpServletRequest request) {
         Long id = systemAreaService.edit(param);
         return ResponseResult.success(id);
     }

     @ApiOperation(value="查看详情")
     @PreAuthorize("@ss.hasPermi('system:area:detail')")
     @RequestMapping(value = "/detail", method = RequestMethod.POST)
     public ResponseResult<SystemAreaDetailResult> detail(@RequestBody @Validated Idparam idparam) {
         SystemAreaDetailResult result = new SystemAreaDetailResult();
         SystemArea entity = systemAreaService.getById(idparam.getId());
         BeanUtils.copyProperties(entity, result);
         return ResponseResult.success(result);
     }

     @ApiOperation(value="删除")
     @PreAuthorize("@ss.hasPermi('system:area:del')")
     @SysOperationLog(value = "删除", model = SysModel.AREA_MANAGE)
     @RequestMapping(value = "/del", method = RequestMethod.POST)
     public ResponseResult del(@RequestBody @NotEmpty(message = "ids 不能为空") List<@NotNull(message = "ids 不能为空") Long> ids, HttpServletRequest request) {
         systemAreaService.del(ids);
         return ResponseResult.success();
     }

    @ApiOperation(value="获取区域树")
    @RequestMapping(value = "/tree", method = RequestMethod.POST)
    public ResponseResult<List<SystemAreaTreeResult>> tree(@RequestBody Idparam idparam, HttpServletRequest request) {
        List<SystemArea> orgs=systemAreaService.getAreaTree(idparam.getId());
        List<SystemAreaTreeResult> result = new ArrayList();
        for(SystemArea area: orgs){
            SystemAreaTreeResult treeItem = new SystemAreaTreeResult();
            BeanUtils.copyProperties(area, treeItem);
            result.add(treeItem);
        }
        return ResponseResult.success(result);
    }

}